﻿namespace Vdc.Grid
{
    using Vdc.Grid.Design;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [PersistChildren(false), ToolboxData("<{0}:Grid runat=server></{0}:Grid>"), ParseChildren(true)]
    public class Column : BoundField, ICloneable
    {
        private FilterOptionsCollection _FilterOptions;
        private ColumnTemplateSettings _templateSettings = new ColumnTemplateSettings();
        internal bool AllowNulls;
        private bool allowTemplateBinding = true;
        private int CurrentRowIndex;
        private bool editTemplateApplied;
        internal string FilterControlID;
        internal string FilterControlPropertyName;
        internal string FilterOptionsDdlID;
        internal List<Hashtable> filterOptionsTemplateContainers;
        internal Vdc.Grid.Grid GridContainer;
        internal bool isHelperForScrolling;
        internal int MaxLength;
        internal SortOrderType RealSortOrder;
        private GridRuntimeTemplate runtimeTemplate;
        internal string TempDataField;
        private GridTemplate template;
        internal string Type;

        public Column()
        {
            this.ConvertEmptyStringToNull = false;
            this.FilterCriteria = new Vdc.Grid.FilterCriteria();
        }

        public object Clone()
        {
            return new Column { 
                Align = this.Align, AllowDelete = this.AllowDelete, AllowEdit = this.AllowEdit, AllowFilter = this.AllowFilter, AllowGroupBy = this.AllowGroupBy, AllowSorting = this.AllowSorting, ApplyFormatInEditMode = this.ApplyFormatInEditMode, DataField = this.DataField, DataFormatString = this.DataFormatString, DataFormatString_GroupHeader = this.DataFormatString_GroupHeader, ExportAsText = this.ExportAsText, GroupBy = this.GroupBy, GroupByPriority = this.GroupByPriority, HeaderAlign = this.HeaderAlign, HeaderText = this.HeaderText, HtmlEncode = this.HtmlEncode, 
                Index = this.Index, ParseHTML = this.ParseHTML, ReadOnly = this.ReadOnly, ShowFilterCriterias = this.ShowFilterCriterias, ShowHeader = this.ShowHeader, SortExpression = this.SortExpression, SortOrder = this.SortOrder, SortPriority = this.SortPriority, Visible = base.Visible, Width = this.Width, Wrap = this.Wrap, TemplateSettings = this.TemplateSettings.Clone() as ColumnTemplateSettings
             };
        }

        internal void CopyRelevantPropertiesFromColumn(Column oColumn)
        {
            this.Align = oColumn.Align;
            this.HeaderAlign = oColumn.HeaderAlign;
            this.DataField = oColumn.DataField;
            this.AllowEdit = oColumn.AllowEdit;
            this.AllowDelete = oColumn.AllowDelete;
            this.HeaderText = oColumn.HeaderText;
            this.Wrap = oColumn.Wrap;
            this.ParseHTML = oColumn.ParseHTML;
            this.AllowFilter = oColumn.AllowFilter;
            this.ShowFilterCriterias = oColumn.ShowFilterCriterias;
            this.AllowGroupBy = oColumn.AllowGroupBy;
            this._FilterOptions = oColumn._FilterOptions;
            this._templateSettings = oColumn._templateSettings;
        }

        public virtual bool Equals(Column col)
        {
            return (((((!(this.Align != col.Align) && (this.AllowDelete == col.AllowDelete)) && ((this.AllowEdit == col.AllowEdit) && (this.AllowFilter == col.AllowFilter))) && (((this.AllowGroupBy == col.AllowGroupBy) && (this.ApplyFormatInEditMode == col.ApplyFormatInEditMode)) && (!(this.DataField != col.DataField) && !(this.DataFormatString != col.DataFormatString)))) && (((!(this.DataFormatString_GroupHeader != col.DataFormatString_GroupHeader) && (this.GroupBy == col.GroupBy)) && ((this.GroupByPriority == col.GroupByPriority) && !(this.HeaderAlign != col.HeaderAlign))) && ((!(this.HeaderText != col.HeaderText) && (this.HtmlEncode == col.HtmlEncode)) && ((this.Index == col.Index) && (this.ParseHTML == col.ParseHTML))))) && ((((this.ReadOnly == col.ReadOnly) && (this.ShowFilterCriterias == col.ShowFilterCriterias)) && ((this.ShowHeader == col.ShowHeader) && !(this.SortExpression != col.SortExpression))) && (((this.SortOrder == col.SortOrder) && (this.SortPriority == col.SortPriority)) && (((base.Visible == col.Visible) && !(this.Width != col.Width)) && (this.Wrap == col.Wrap)))));
        }

        internal object GetCellValue(Control controlContainer)
        {
            return base.GetValue(controlContainer);
        }

        internal virtual string GetDefaultFilterOptionID()
        {
            foreach (FilterOption option in this.FilterOptions)
            {
                if (option.IsDefault)
                {
                    return option.GetID();
                }
            }
            if (!this.ShowFilterCriterias)
            {
                return "1";
            }
            return "0";
        }

        internal FilterOption GetFilterOptionById(string id)
        {
            foreach (FilterOption option in this.FilterOptions)
            {
                if (option.GetID() == id)
                {
                    return option;
                }
            }
            return null;
        }

        internal List<FilterOption> GetFilterOptions()
        {
            if (this.FilterOptions.Count == 0)
            {
                return StandardFilterCriterias.GetStandardFilterOptions();
            }
            List<FilterOption> list = new List<FilterOption>();
            foreach (FilterOption option in this.FilterOptions)
            {
                list.Add(option);
            }
            return list;
        }

        internal string GetFilterOptionsTemplateIds()
        {
            StringBuilder builder = new StringBuilder();
            int num = 0;
            builder.Append("[");
            if (this.filterOptionsTemplateContainers != null)
            {
                foreach (Hashtable hashtable in this.filterOptionsTemplateContainers)
                {
                    if (num > 0)
                    {
                        builder.Append(",");
                    }
                    HtmlContainer container = hashtable["templateContainer"] as HtmlContainer;
                    builder.Append("[");
                    builder.Append("\"" + hashtable["customFilterOptionId"] + "\",");
                    builder.Append("\"" + container.ClientID + "\"");
                    builder.Append("]");
                    num++;
                }
            }
            builder.Append("]");
            return builder.ToString();
        }

        protected override object GetValue(Control controlContainer)
        {
            object obj2 = base.GetValue(controlContainer);
            if (string.IsNullOrEmpty(this.NullDisplayText) || ((obj2 != DBNull.Value) && (!string.IsNullOrEmpty(obj2 as string) || !this.ConvertEmptyStringToNull)))
            {
                return obj2;
            }
            return this.NullDisplayText;
        }

        internal bool HasCustomFilterOptionsWithTemplates()
        {
            foreach (FilterOption option in this.FilterOptions)
            {
                if (option is CustomFilterOption)
                {
                    CustomFilterOption option2 = option as CustomFilterOption;
                    if (!string.IsNullOrEmpty(option2.TemplateSettings.FilterTemplateId))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                object[] objArray = savedState as object[];
                if ((objArray != null) && (objArray.Length == 2))
                {
                    base.LoadViewState(objArray[0]);
                    if (objArray[1] != null)
                    {
                        ((IStateManager) this.TemplateSettings).LoadViewState(objArray[1]);
                    }
                }
            }
            else
            {
                base.LoadViewState(savedState);
            }
        }

        protected override object SaveViewState()
        {
            object[] objArray = new object[2];
            objArray[0] = base.SaveViewState();
            if (this._templateSettings != null)
            {
                objArray[1] = ((IStateManager) this.TemplateSettings).SaveViewState();
            }
            return objArray;
        }

        private bool ShouldSerializeTemplateSettings()
        {
            return true;
        }

        object ICloneable.Clone()
        {
            return this.Clone();
        }

        protected override void TrackViewState()
        {
            base.TrackViewState();
            if (this._templateSettings != null)
            {
                ((IStateManager) this.TemplateSettings).TrackViewState();
            }
        }

        [Description("Align of the Column."), DefaultValue("left"), Bindable(true), Category("Column Settings")]
        public string Align
        {
            get
            {
                object obj2 = base.ViewState["Align"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "left";
            }
            set
            {
                base.ViewState["Align"] = value;
            }
        }

        [Description("Show/Hide the Delete link"), Bindable(true), Category("Column Settings"), DefaultValue(false)]
        public bool AllowDelete
        {
            get
            {
                object obj2 = base.ViewState["AllowDelete"];
                return ((obj2 != null) && ((bool) obj2));
            }
            set
            {
                base.ViewState["AllowDelete"] = value;
            }
        }

        [Bindable(true), DefaultValue(false), Description("Show/Hide the Edit link"), Category("Column Settings")]
        public bool AllowEdit
        {
            get
            {
                object obj2 = base.ViewState["AllowEdit"];
                return ((obj2 != null) && ((bool) obj2));
            }
            set
            {
                base.ViewState["AllowEdit"] = value;
            }
        }

        [DefaultValue(true), Category("Column Settings"), Description("Enables/disables filtering for the column."), Bindable(true)]
        public bool AllowFilter
        {
            get
            {
                object obj2 = base.ViewState["AllowFilter"];
                if (obj2 != null)
                {
                    return (bool) obj2;
                }
                return true;
            }
            set
            {
                base.ViewState["AllowFilter"] = value;
            }
        }

        [Description("Enables/disables the grouping feature for the column."), Bindable(true), Category("Column Settings"), DefaultValue(true)]
        public bool AllowGroupBy
        {
            get
            {
                object obj2 = base.ViewState["AllowGroupBy"];
                if (obj2 != null)
                {
                    return (bool) obj2;
                }
                return true;
            }
            set
            {
                base.ViewState["AllowGroupBy"] = value;
            }
        }

        [Bindable(true), Category("Column Settings"), DefaultValue(true), Description("Gets or sets a value indicating whether sorting is enabled for the column.")]
        public bool AllowSorting
        {
            get
            {
                object obj2 = base.ViewState["AllowSorting"];
                if (obj2 != null)
                {
                    return (bool) obj2;
                }
                return true;
            }
            set
            {
                base.ViewState["AllowSorting"] = value;
            }
        }

        [DefaultValue(false), Bindable(true), Category("Column Settings")]
        public override bool ConvertEmptyStringToNull
        {
            get
            {
                return base.ConvertEmptyStringToNull;
            }
            set
            {
                base.ConvertEmptyStringToNull = value;
            }
        }

        [Bindable(true), Description("The DataFormatString used for the group header."), Category("Column Settings"), DefaultValue("")]
        public string DataFormatString_GroupHeader
        {
            get
            {
                object obj2 = base.ViewState["DataFormatString_GroupHeader"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return this.DataFormatString;
            }
            set
            {
                base.ViewState["DataFormatString_GroupHeader"] = value;
            }
        }

        [Obsolete("This property is obsolete. Please use the TemplateSettings property to set the properties related to Templates for the Column.", false), Bindable(true), Browsable(false), Category("Column Settings"), DefaultValue(""), Description("The id of the template that is used for the column in edit mode.")]
        public string EditTemplateId
        {
            get
            {
                return this.TemplateSettings.EditTemplateId;
            }
            set
            {
                this.TemplateSettings.EditTemplateId = value;
            }
        }

        [Description("Gets or sets a value indicating whether the data from the column should be exported as text."), Bindable(true), Category("Column Settings"), DefaultValue(false)]
        public bool ExportAsText
        {
            get
            {
                object obj2 = base.ViewState["ExportAsText"];
                return ((obj2 != null) && ((bool) obj2));
            }
            set
            {
                base.ViewState["ExportAsText"] = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Category("Column Settings"), Description("Gets/sets the filter criteria for the column."), Bindable(true), Browsable(false)]
        public Vdc.Grid.FilterCriteria FilterCriteria
        {
            get
            {
                object obj2 = base.ViewState["FilterCriteria"];
                if (obj2 != null)
                {
                    return (Vdc.Grid.FilterCriteria) obj2;
                }
                return null;
            }
            set
            {
                base.ViewState["FilterCriteria"] = value;
            }
        }

        [Category("Column Filter Options"), NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty), Bindable(true), Description("Collection of FilterOption objects.")]
        public FilterOptionsCollection FilterOptions
        {
            get
            {
                if (this._FilterOptions == null)
                {
                    this._FilterOptions = new FilterOptionsCollection();
                }
                return this._FilterOptions;
            }
        }

        [Obsolete("This property is obsolete. Please use the TemplateSettings property to set the properties related to Templates for the Column.", false), Description("The id of the template that is used for the filter of the column."), Bindable(true), Browsable(false), Category("Column Settings"), DefaultValue("")]
        public string FilterTemplateId
        {
            get
            {
                return this.TemplateSettings.FilterTemplateId;
            }
            set
            {
                this.TemplateSettings.FilterTemplateId = value;
            }
        }

        [Description("The GroupBy for the column."), Category("Column Settings"), Bindable(true), DefaultValue("")]
        internal bool GroupBy
        {
            get
            {
                object obj2 = base.ViewState["GroupBy"];
                return ((obj2 != null) && ((bool) obj2));
            }
            set
            {
                base.ViewState["GroupBy"] = value;
            }
        }

        [Category("Column Settings"), DefaultValue("0"), Description("The priority of the Column in the GroupBy expression."), Bindable(true)]
        internal int GroupByPriority
        {
            get
            {
                object obj2 = base.ViewState["GroupByPriority"];
                if (obj2 != null)
                {
                    return (int) obj2;
                }
                return 0;
            }
            set
            {
                base.ViewState["GroupByPriority"] = value;
            }
        }

        [Bindable(true), Description("Align attribute for the header of the column."), Category("Column Settings"), DefaultValue("left")]
        public string HeaderAlign
        {
            get
            {
                object obj2 = base.ViewState["HeaderAlign"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "left";
            }
            set
            {
                base.ViewState["HeaderAlign"] = value;
            }
        }

        [Category("Column Settings"), Obsolete("This property is obsolete. Please use the TemplateSettings property to set the properties related to Templates for the Column.", false), Bindable(true), Browsable(false), DefaultValue(""), Description("The id of the template that is used for the header of the column.")]
        public string HeaderTemplateId
        {
            get
            {
                return this.TemplateSettings.HeaderTemplateId;
            }
            set
            {
                this.TemplateSettings.HeaderTemplateId = value;
            }
        }

        [Bindable(true), Description("Text used for the header of the column."), Category("Column Settings"), DefaultValue("")]
        public override string HeaderText
        {
            get
            {
                object obj2 = base.ViewState["HeaderText"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return this.DataField;
            }
            set
            {
                base.ViewState["HeaderText"] = value;
            }
        }

        [Category("Column Settings"), Description("The ID of the column"), Bindable(true), DefaultValue(""), Browsable(false)]
        public string ID
        {
            get
            {
                object obj2 = base.ViewState["ID"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "";
            }
            set
            {
                base.ViewState["ID"] = value;
            }
        }

        [Bindable(true), Description("The index of the column"), Category("Column Settings"), DefaultValue(-1)]
        public int Index
        {
            get
            {
                object obj2 = base.ViewState["Index"];
                if (obj2 != null)
                {
                    return (int) obj2;
                }
                return -1;
            }
            set
            {
                base.ViewState["Index"] = value;
            }
        }

        [DefaultValue(false), Category("Column Settings"), Bindable(true), Description("Enables/disables html parsing for the column.")]
        public bool ParseHTML
        {
            get
            {
                object obj2 = base.ViewState["ParseHTML"];
                return ((obj2 != null) && ((bool) obj2));
            }
            set
            {
                base.ViewState["ParseHTML"] = value;
            }
        }

        [Bindable(true), DefaultValue(false), Description(""), Category("Column Settings")]
        internal bool RealVisible
        {
            get
            {
                object obj2 = base.ViewState["RealVisible"];
                if (obj2 != null)
                {
                    return (bool) obj2;
                }
                return base.Visible;
            }
            set
            {
                base.ViewState["RealVisible"] = value;
            }
        }

        [Category("Column Settings"), DefaultValue(true), Description("Show/Hide the Filter Criterias Selector."), Bindable(true)]
        public bool ShowFilterCriterias
        {
            get
            {
                object obj2 = base.ViewState["ShowFilterCriterias"];
                if (obj2 != null)
                {
                    return (bool) obj2;
                }
                return true;
            }
            set
            {
                base.ViewState["ShowFilterCriterias"] = value;
            }
        }

        [Bindable(true), DefaultValue(0), Description("The SortOrder for the column."), Category("Column Settings")]
        public SortOrderType SortOrder
        {
            get
            {
                object obj2 = base.ViewState["SortOrder"];
                if (obj2 != null)
                {
                    return (SortOrderType) obj2;
                }
                return SortOrderType.None;
            }
            set
            {
                base.ViewState["SortOrder"] = value;
            }
        }

        [DefaultValue(0), Category("Column Settings"), Description("The priority of the Column in the sort expression."), Bindable(true)]
        public int SortPriority
        {
            get
            {
                object obj2 = base.ViewState["SortPriority"];
                if (obj2 != null)
                {
                    return (int) obj2;
                }
                return 0;
            }
            set
            {
                base.ViewState["SortPriority"] = value;
            }
        }

        [Category("Column Settings"), Description("The id of the template that is used for the column."), Bindable(true), Browsable(false), Obsolete("This property is obsolete. Please use the TemplateSettings property to set the properties related to Templates for the Column.", false), DefaultValue("")]
        public string TemplateId
        {
            get
            {
                return this.TemplateSettings.TemplateId;
            }
            set
            {
                this.TemplateSettings.TemplateId = value;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty), Description("Property used to define the Templates related settings for the Column."), NotifyParentProperty(true), Editor(typeof(ColumnTemplateSettingsEditor), typeof(UITypeEditor)), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Bindable(true), Category("Column Template Settings")]
        public ColumnTemplateSettings TemplateSettings
        {
            get
            {
                return this._templateSettings;
            }
            set
            {
                this._templateSettings = value;
            }
        }

        [Category("Column Settings"), DefaultValue("175"), Description("Width of the Column."), Bindable(true)]
        public string Width
        {
            get
            {
                object obj2 = base.ViewState["Width"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "175";
            }
            set
            {
                string str = value.Replace("px", "");
                if (string.IsNullOrEmpty(str))
                {
                    str = "175";
                }
                base.ViewState["Width"] = str;
            }
        }

        [Description("Enable/disable text wrapping on multiple lines for the column."), Bindable(true), Category("Column Settings"), DefaultValue(false)]
        public bool Wrap
        {
            get
            {
                object obj2 = base.ViewState["Wrap"];
                return ((obj2 != null) && ((bool) obj2));
            }
            set
            {
                base.ViewState["Wrap"] = value;
            }
        }
    }
}

